﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

using System.Text;
using System.IO;
using System.Net.Sockets;

using System.Web.Mail;
public partial class carinsurance_CreateCustomer : System.Web.UI.Page
{
    private enum SMTPResponse : int
    {
        CONNECT_SUCCESS = 220,
        GENERIC_SUCCESS = 250,
        DATA_SUCCESS = 354,
        QUIT_SUCCESS = 221

    }

    private static void Senddata(Socket s, string msg)
    {

        byte[] _msg = Encoding.ASCII.GetBytes(msg);
        s.Send(_msg, 0, _msg.Length, SocketFlags.None);
    }

    private static bool Check_Response(Socket s, SMTPResponse response_expected)
    {
        string sResponse;
        int response;
        byte[] bytes = new byte[1024];
        while (s.Available == 0)
        {
            System.Threading.Thread.Sleep(100);
        }

        s.Receive(bytes, 0, s.Available, SocketFlags.None);
        sResponse = Encoding.ASCII.GetString(bytes);
        response = Convert.ToInt32(sResponse.Substring(0, 3));
        if (response != (int)response_expected)
            return false;
        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int _endYear = DateTime.Now.Year - 18;
            int _startYear = DateTime.Now.Year - 75;
            for (int i = _endYear; i >= _startYear; i--)
            {
                ddlYear.Items.Add(i.ToString());
            }
        }
    }

    protected void btnCheckEmailAddress_Click(object sender, EventArgs e)
    {
        rfvEmail.Validate();
        revEmail.Validate();
        if (rfvEmail.IsValid && revEmail.IsValid)
        {
            if (DNSLookup())
            {
                lblEmailStatus.Text = "Email address is okayed.";
            }
            else
            {
                return;
            }


            IPHostEntry IPhst = Dns.Resolve("mail.comparexpress.co");
            IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
            Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            
            s.Connect(endPt);
            
            Senddata(s, string.Format("HELO {0}\r\n", Dns.GetHostName()));
            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
            {
                s.Close();
                lblEmailStatus.Text = "HELO Send failed.";
                return;
            }

            lblEmailStatus.Text = "";
            if (!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))
            {
                s.Close();
                lblEmailStatus.Text = "SMPT Server Failed Connection.";
                return;
            }

            MailMessage _m = new MailMessage();
            _m.Subject = "Testing";
            _m.From = "noreply@comparexpress.co";
            _m.To = txtEmailAddress.Text;
            _m.Body = "Testing";
            _m.Headers.Add("Reply-to", "phyokyawthant@gmail.com");

            Senddata(s, string.Format("RCPT TO: {0}\r\n", _m.To));
            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
            {
                s.Close();
                lblEmailStatus.Text = "To address failed to send.";
                return;
            }

            lblEmailStatus.Text = "To Email is okayed for all steps.";
        }
    }

    //     1. Protected Sub DNSLookup()  
    // 2.     Try  
    // 3.         'performs the DNS lookup  
    // 4.         Dim objmail As New MailAddress("fullemail")  
    // 5.   
    // 6.         Dim ip_addrs As IPAddress() = Dns.GetHostAddresses(objmail.Host)  
    // 7.         For Each ip As IPAddress In ip_addrs  
    // 8.             txtIps.Text = ip.ToString()  
    // 9.         Next ip  
    //10.     Catch ex As System.Exception  
    //11.         lblStatus.Text = ex.ToString()  
    //12.     End Try  
    //13. End Sub  
    //14.   
    //15. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click  
    //16.     DNSLookup()  
    //17. End Sub  

    protected bool DNSLookup()
    {
        bool _retVal = false;
        try
        {
            txtIps.Text = "";
            System.Net.Mail.MailAddress _objMail = new System.Net.Mail.MailAddress(txtEmailAddress.Text);
            IPAddress[] _ipAddress = Dns.GetHostAddresses(_objMail.Host.ToString());
            foreach (IPAddress _ipTemp in _ipAddress)
            {
                txtIps.Text += _ipTemp.ToString();
            }
            System.Net.Mail.SmtpClient _smtp = new System.Net.Mail.SmtpClient(_objMail.Host);

            _retVal = true;
        }
        catch (System.Exception Ex)
        {
            lblEmailStatus.Text = Ex.ToString();
        }
        return _retVal;
    }

    protected void btnNewSubmit_Click(object sender, EventArgs e)
    {
        txtFirstName.Text = lblMessage.Text;
    }
}
